#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    this->setWindowTitle("简易计算器");
    /* 初始化成员变量 */
    //数字按钮组
    this->btns[0] = ui->btn_zero;
    this->btns[1] = ui->btn_one;
    this->btns[2] = ui->btn_two;
    this->btns[3] = ui->btn_tree;
    this->btns[4] = ui->btn_four;
    this->btns[5] = ui->btn_five;
    this->btns[6] = ui->btn_six;
    this->btns[7] = ui->btn_seven;
    this->btns[8] = ui->btn_eight;
    this->btns[9] = ui->btn_nine;
    //小数点按钮
    this->btn_point = ui->btn_point;
    //等号按钮
    this->btn_equal = ui->btn_equal;
    //加减乘除按钮
    this->btn_add = ui->btn_add;
    this->btn_sub = ui->btn_sub;
    this->btn_mul = ui->btn_mul;
    this->btn_div = ui->btn_div;
    //清空按钮
    this->btn_clear = ui->btn_clear;
    //删除按钮
    this->btn_del = ui->btn_del;

    //标签
    this->label_expression = ui->label_expression;
    this->label_result = ui->label_result;

    //切换窗口
    this->btn_change = ui->btn_change;
    this->btn_complex = ui->btn_complex;
    this->btn_set = ui->btn_set;
    this->btn_matrix = ui->btn_matrix;
    /* 为按钮绑定单击事件 */
    //数字按钮单击事件：在label_expression 追加对应数字
    for(int i = 0; i < 10; i++){
        connect(btns[i], &QPushButton::clicked, [=]{
            //表示为0则置为i,否则拼接
            QString str = label_expression->text();
            if(str == "0"){
                label_expression->setText(QString::number(i));
            }else{
                label_expression->setText(label_expression->text().append(QString::number(i)));
            }
        });
    }
    //小数点按钮单击事件
    connect(btn_point, &QPushButton::clicked, [=]{
       label_expression->setText(label_expression->text().append(QString(".")));
    });
    //清空按钮单击事件
    connect(btn_clear, &QPushButton::clicked, [=]{
        //表达式置0
        label_expression->setText(QString("0"));
        //结果清空
        label_result->setText(QString());
    });
    //删除按钮单击事件
    connect(btn_del, &QPushButton::clicked, [=]{
        //表达式长度大于1，删除最后一个字符，否则置0
        QString str = label_expression->text();
        if(str.length() > 1){
            label_expression->setText(str.remove(str.length() - 1, 1));
        }else{
            if(str != "0"){
                label_expression->setText("0");
            }
        }
    });

    //加减乘除
    connect(btn_add, &QPushButton::clicked, [=]{
        QString str = label_expression->text();
        if(Util::isLastCharAnOperator(str)){
            label_expression->setText(str.replace(str.length() - 1, 1, '+'));
        }else{
            label_expression->setText(label_expression->text().append("+"));
        }

    });
    connect(btn_sub, &QPushButton::clicked, [=]{
        QString str = label_expression->text();
        if(Util::isLastCharAnOperator(str)){
            label_expression->setText(str.replace(str.length() - 1, 1, '-'));
        }else{
            label_expression->setText(label_expression->text().append("-"));
        }
    });
    connect(btn_mul, &QPushButton::clicked, [=]{
        QString str = label_expression->text();
        if(Util::isLastCharAnOperator(str)){
            label_expression->setText(str.replace(str.length() - 1, 1, '*'));
        }else{
            label_expression->setText(label_expression->text().append("*"));
        }
    });
    connect(btn_div, &QPushButton::clicked, [=]{
        QString str = label_expression->text();
        if(Util::isLastCharAnOperator(str)){
            label_expression->setText(str.replace(str.length() - 1, 1, '/'));
        }else{
            label_expression->setText(label_expression->text().append("/"));
        }
    });

    connect(btn_equal, &QPushButton::clicked, [=]{
        double res;
//        QString s = label_expression->text();
//        qDebug()<<s<<endl;
        bool success = service.expressionCalculate(label_expression->text(), res);
        if(success){
            QString resStr = QString::number(res, 'f', service.precision);
            resStr = Util::dealDoubleStr(resStr);
            label_result->setText("=" + resStr);
            label_expression->setText(resStr);
        }else{
            label_result->setText("ERROR");
        }
    });
    //切换按钮单击事件
    connect(btn_change, &QPushButton::clicked, [=]{
        this->hide();
        //设置新窗口的位置为当前窗口的位置
        ApplicationManager::windows[1]->setGeometry(this->geometry());
        ApplicationManager::windows[1]->show();
    });
    connect(btn_complex, &QPushButton::clicked, [=]{
        this->hide();
        //设置新窗口的位置为当前窗口的位置
        ApplicationManager::windows[1]->setGeometry(this->geometry());
        ApplicationManager::windows[1]->show();
    });
    connect(btn_matrix, &QPushButton::clicked, [=]{
        this->hide();
        //设置新窗口的位置为当前窗口的位置
        ApplicationManager::windows[2]->setGeometry(this->geometry());
        ApplicationManager::windows[2]->show();
    });
    connect(btn_set, &QPushButton::clicked, [=]{
        this->hide();
        //设置新窗口的位置为当前窗口的位置
        ApplicationManager::windows[3]->setGeometry(this->geometry());
        ApplicationManager::windows[3]->show();
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

